/**
 * Created by kangli on 2018/4/7.
 */
var newsTypeId1=parent.$(".dropdown-menu1").eq(0).find("li.active").attr("data-type");
var sum;
var counts='';
var pageThis =0;
var pagecount = 8;
var index = 0;

var newsLists = ''

$(function(){
	newsTyped1=parent.$(".dropdown-menu1").eq(0).find("li.active").attr("data-type");
    getPhotoNews();
})
//得到图片新闻
function getPhotoNews(){
    var  data={newsTypeId:newsTypeId1,page: index ,size: pagecount};
    AjaxSubmit("GET",data, basePath + "/super/common/resource/getNews",getNews_fun);
}

function getNews_fun(res){
   /* console.log(res);*/
    if(res.status=='SUCCESS'){
        newsLists = res.obj.content;
        sum = res.obj.totalElements;
        counts = sum / pagecount;
        if(newsTypeId1==1){//图片新闻
            if (newsLists.length != 0) {
                $(".photoThead").empty();
                var str =' <tr> ' +
                    '<th>封面</th> ' +
                    '<th>标题</th> ' +
                    '<th>摘要</th> ' +
                    '<th>创建日期</th> ' +
                    '<th class="texC">状态</th> ' +
                    '<th class="text-center">操作</th> </tr>';

                $(".photoThead").append(str);
                if (sum % pagecount == 0) {
                    counts = parseInt(sum / pagecount);
                }
                else {
                    counts = parseInt(sum / pagecount) + 1;
                }
                $("#photoPage").paging({
                    pageNo: pageThis,
                    totalPage: counts,
                    totalSize: sum,
                    callback: function (num) {
                       /* console.log(num);*/
                        index = num - 1;
                        getPhotoNews();
                        pageThis = num;
                    }
                });
                $(".photoTbody").empty();
                $('.warnDiv7').remove();
                $.each(newsLists, function (i, newsList) {
                    var statustr = '';
                    if(newsList.status=='DOWN'){//未发布
                        statustr='未发布 |<span class="cursorP colorBlue" data-id="'+newsList.id+'" data-newsTypeId1="1" onclick="changeStatu(this)"> 发布</span>'
                    }else if(newsList.status=='UP'){ //已发布
                        statustr='已发布 |<span class="cursorP colorBlue" data-id="'+newsList.id+'" data-newsTypeId1="1" onclick="changeStatu(this)"> 关闭</span>'
                    }
                    var pic=newsList.pic||'';
                    var createTime = newsList.creatTime || '';
                    var zhaiyao = newsList.abstractContent || '';
                    var html = $('<tr> ' +
                        '<td><img src="'+pic+'"></td> ' +
                        '<td>'+newsList.title+'</td> ' +
                        '<td>'+zhaiyao+'</td> ' +
                        '<td>'+createTime+'</td> ' +
                        '<td class="texC">'+statustr+'</td> ' +
                        '<td class="texC "> <a href="../informationMana/addNews.html?&newsGet=1&type=news&newsTypeId='+newsTypeId1+'&needUpdate=1&nid='+newsList.id+'" class="texC  cursorP colorBlue">编辑</a>  | <span class=" cursorP colorBlue" data-id="'+newsList.id+'" onclick="deleteNews(this)">删除</span></td> ' +
                        '</tr>');
                    $(".photoTbody").append(html)
                });
            }
            else{
                $(".photoTbody").empty();
                $('.warnDiv7').remove();
                var html = $('<div class="text-center warnDiv7 "  style="width: 100%;margin: 73px 0px;font-size: 14px" >无信息!</div>');
                $(".photoTable").after(html)
            }
        }
        else if(newsTypeId1==2){
            if (newsLists.length != 0) {
                $(".zhejiangThead").empty();
                var str1 =' <tr> ' +
                    '<th>封面</th> ' +
                    '<th>标题</th> ' +
                    '<th>摘要</th> ' +
                    '<th>创建日期</th> ' +
                    '<th class="texC">状态</th> ' +
                    '<th class="text-center">操作</th> ' +
                    '</tr>';

                $(".zhejiangThead").append(str1);
                if (sum % pagecount == 0) {
                    counts = parseInt(sum / pagecount);
                }
                else {
                    counts = parseInt(sum / pagecount) + 1;
                }
                $("#zhejiangPage").paging({
                    pageNo: pageThis,
                    totalPage: counts,
                    totalSize: sum,
                    callback: function (num) {
                        /*console.log(num);*/
                        index = num - 1;
                        getPhotoNews();
                        pageThis = num;
                    }
                });
                $(".zhejiangTBody").empty();
                $('.warnDiv6').remove();
                $.each(newsLists, function (i, newsList) {
                    var statustr = '';
                    if(newsList.status=='DOWN'){//未发布
                        statustr='未发布 |<span class="cursorP colorBlue" data-id="'+newsList.id+'" data-newsTypeId1="2" onclick="changeStatu(this)"> 发布</span>'
                    }else if(newsList.status=='UP'){ //已发布
                        statustr='已发布 |<span class="cursorP colorBlue" data-id="'+newsList.id+'" data-newsTypeId1="2" onclick="changeStatu(this)"> 关闭</span>'
                    }
                    var zhaiyao = newsList.abstractContent || '';
                    var createTime = newsList.creatTime || '';
                    var pic=newsList.pic||'';
                    var html = $('<tr> ' +
                        '<td><img src="'+pic+'"></td> ' +
                        '<td>'+newsList.title+'</td> ' +
                        '<td>'+zhaiyao+'</td> ' +
                        '<td>'+createTime+'</td> ' +
                        '<td class="texC">'+statustr+'</td> ' +
                        '<td class="texC ">  <a href="../informationMana/addNews.html?newsGet=1&type=news&newsTypeId='+newsTypeId1+'&needUpdate=1&nid='+newsList.id+'"  class="texC  cursorP colorBlue">编辑</a> | <span class=" cursorP colorBlue" data-id="'+newsList.id+'" onclick="deleteNews(this)">删除</span></td> ' +
                        '</tr>');
                    $(".zhejiangTBody").append(html)
                });
            }
            else{
                $(".zhejiangTBody").empty();
                $('.warnDiv6').remove();
                var html1 = $('<div class="text-center warnDiv6" style="width: 100%;margin: 73px 0px;font-size: 14px" >无信息!</div>');
                $(".zhejiangTable").after(html1)
            }
        }
        else if(newsTypeId1==3){
            if (newsLists.length != 0) {
                $(".currentThead").empty();
                var str2 =' <tr> ' +
                    '<th>封面</th> ' +
                    '<th>标题</th> ' +
                    '<th>摘要</th> ' +
                    '<th>创建日期</th> ' +
                    '<th class="texC">状态</th> ' +
                    '<th class="text-center">操作</th> ' +
                    '</tr>';

                $(".currentThead").append(str2);
                if (sum % pagecount == 0) {
                    counts = parseInt(sum / pagecount);
                }
                else {
                    counts = parseInt(sum / pagecount) + 1;
                }
                $("#currentPage").paging({
                    pageNo: pageThis,
                    totalPage: counts,
                        totalSize: sum,
                        callback: function (num) {
                        /*  console.log(num);*/
                        index = num - 1;
                        getPhotoNews();
                        pageThis = num;
                    }
                });
                $(".currentTbody").empty();
                $('.warnDiv5').remove();
                $.each(newsLists, function (i, newsList) {
                    var statustr = '';
                    if(newsList.status=='DOWN'){//未发布
                        statustr='未发布 |<span class="cursorP colorBlue" data-id="'+newsList.id+'" data-newsTypeId1="3" onclick="changeStatu(this)"> 发布</span>'
                    }else if(newsList.status=='UP'){ //已发布
                        statustr='已发布 |<span class="cursorP colorBlue" data-id="'+newsList.id+'" data-newsTypeId1="3" onclick="changeStatu(this)"> 关闭</span>'
                    }
                    var zhaiyao = newsList.abstractContent || '';
                    var createTime = newsList.creatTime || '';
                    var pic=newsList.pic||'';
                    var html = $('<tr> ' +
                        '<td><img src="'+pic+'"></td> ' +
                        '<td>'+newsList.title+'</td> ' +
                        '<td>'+zhaiyao+'</td> ' +
                        '<td>'+createTime+'</td> ' +
                        '<td class="texC">'+statustr+'</td> ' +
                        '<td class="texC ">  <a href="../informationMana/addNews.html?newsGet=1&type=news&newsTypeId='+newsTypeId1+'&needUpdate=1&nid='+newsList.id+'"  class="texC  cursorP colorBlue">编辑</a> | <span class=" cursorP colorBlue" data-id="'+newsList.id+'" onclick="deleteNews(this)" >删除</span></td> ' +
                        '</tr>');
                    $(".currentTbody").append(html)
                });
            }
            else{
                $(".currentTbody").empty();
                $('.warnDiv5').remove();
                var html2 = $('<div class="text-center warnDiv5" style="width: 100%;margin: 73px 0px;font-size: 14px" >无信息!</div>');
                $(".currentTable").after(html2)
            }
        }
    }
}
//删除新闻
function deleteNews(btn){
    var msg = "您真的确定要删除吗？\n\n请确认！";
    if (confirm(msg)==true){
        var newsId = $(btn).attr("data-id");
        var deldata = {newsId:newsId};
       /* console.log(deldata)*/
        AjaxSubmit("POST",deldata, basePath + "/super/common/resource/deleteNews",deleteNews_fun);
    }else{
        return false;
    }
}

function deleteNews_fun(res){
    /*console.log(res);*/
   if(res.status=='SUCCESS'){
       index = $(".pagination").find(".active").find("a").text()-1;
       /*console.log(index);*/
       getPhotoNews();
   }
}

//改变状态
function changeStatu(btn){
    var newsId = $(btn).attr("data-id");
    var  data={id: newsId};
  /*  console.log(data)*/
    AjaxSubmit("POST",data, basePath + "/super/common/resource/changeStatus",changeStatus_fun);
    function changeStatus_fun(res){
        /*console.log(res);*/
        if(res.status=='SUCCESS'){
            index = $(".pagination").find(".active").find("a").text()-1;
            newsTypeId1 = $(btn).attr("data-newsTypeId1");
            getPhotoNews();
        }
    }
}
